import { getTodoList , getAllDone} from "@/utils/localStorageTodo"

export default {
    namespaced: true,
    state: {
        todoList: getTodoList(),
        allChecked:getAllDone()
    },
    mutations: {
        ADD_TODOLIST(state, item) { //新增
            state.todoList = [item].concat(state.todoList)
        },
        UPDATE_TODOLIST(state, { text, index }) { //修改
            state.todoList[index].text = text.trim()
        },
        DELETE_TODOLIST(state, index) { //删除
            state.todoList.splice(index, 1)
        },
        SET_CHECKED(state, checked) { //全选反选
            state.todoList.map(v => {
                v.isCheck = !checked
            })
        },
        SET_ALLCHECKED(state) { //单选
            console.log(state.todoList.filter(v=>v.isCheck))
        }
    },
    actions: {
        addTodoList({ commit }, item) { //新增
            commit('ADD_TODOLIST', item)
        },
        updateTodoList({ commit }, { text, index }) { // 修改
            if (!text.trim().length) {
                commit('DELETE_TODOLIST', index)
            } else {
                commit('UPDATE_TODOLIST', { text, index })
            }
        },
        deleteTodoList( {commit},index) { //删除
            commit('DELETE_TODOLIST',index)
        },
        setChecked({ commit }, checked) { //全选反选
            commit('SET_CHECKED', checked)
        },
        setAllChecked({commit}) { //单选
            commit('SET_ALLCHECKED')
        }
    },
}